<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="楼盘名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入楼盘名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-table-column label="物业类型" align="center" prop="premisesType" >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.premisesType==0">普通住宅</el-tag>
          <el-tag v-if="scope.row.premisesType==1">公寓</el-tag>
          <el-tag v-if="scope.row.premisesType==2">商住两用</el-tag>
          <el-tag v-if="scope.row.premisesType==3">商业地产</el-tag>
          <el-tag v-if="scope.row.premisesType==4">写字楼</el-tag>
        </template>
      </el-table-column>
      <el-form-item prop="province">
        <el-select v-model="queryParams.province" placeholder="省" @change="provinceChange">
          <el-option
            v-for="item in regionList"
            :key="item.regionId"
            :label="item.regionName"
            :value="item.regionId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="city">
        <el-select v-model="queryParams.city" placeholder="市" @change="cityChange">
          <el-option
            v-for="item in citys"
            :key="item.regionId"
            :label="item.regionName"
            :value="item.regionId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="county">
        <el-select v-model="queryParams.county" placeholder="区" @change="countryChange">
          <el-option
            v-for="item in countrys"
            :key="item.regionId"
            :label="item.regionName"
            :value="item.regionId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <router-link :to="{path:'/house/add',query: this.ids,queryParams: this.ids }">
          <button>新增</button>
        </router-link>
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['house:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['house:edit']"
        >修改</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="housemanagerList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="楼盘ID" align="center" prop="id" />
      <el-table-column label="楼盘名称" align="center" prop="name" />
      <el-table-column label="楼盘图片" align="center">
        <template scope="scope">
          <el-image :src="scope.row.url" style="width: 100px"></el-image>
        </template>
      </el-table-column>
      <el-table-column label="地址" align="center" prop="address" />
      <el-table-column label="物业类型" align="center" prop="premisesType" >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.premisesType==0">普通住宅</el-tag>
          <el-tag v-if="scope.row.premisesType==1">公寓</el-tag>
          <el-tag v-if="scope.row.premisesType==2">商住两用</el-tag>
          <el-tag v-if="scope.row.premisesType==3">商业地产</el-tag>
          <el-tag v-if="scope.row.premisesType==4">写字楼</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="物业管理公司" align="center" prop="estate" />
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>
<script>
  import { listHousemanager, getHousemanager, delHousemanager, addHousemanager, updateHousemanager } from "@/api/house/housemanager";
  import { listRegion, getRegion} from "@/api/house/region";
  export default {
    name: "index",
    components: {
    },
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 楼盘信息表格数据
        housemanagerList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          name: null,
          province: null,
          city: null,
          county: null,
          premissType: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
        },
        citys:[],
        province: '',
        regionList: [],
        value: '',
        countrys: [],
      };
    },
    created() {
      this.getList();
      this.getRegion();
    },
    methods: {
      /** 查询楼盘信息列表 */
      getList() {
        this.loading = true;
        listHousemanager(this.queryParams).then(response => {
          console.log(response.rows)
          this.housemanagerList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          name: null,
          province: null,
          city: null,
          county: null,
        };
        this.resetForm("form");
      },
      getRegion(){
        this.citys = [];
        this.countrys= [];
        listRegion(this.queryParams).then(response => {
          this.regionList = this.handleTree(response.data, "regionId", "parentId");
        });
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.pid=this.$router.query.name
        if(this.pid !=null){
          this.handleUpdate()
        }
        this.$router.push({
          path: '/house/add',
          query: this.ids,
        })

      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        this.open = true;
        const id = row.id || this.ids
        console.log(id+"111111111111111")
        getHousemanager(id).then(response => {
          this.form = response.data;
          this.$router.push({path:"add",query:{name:id}})
          this.title = "修改楼盘信息";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {

            if (this.form.id != null) {
              updateHousemanager(this.form).then(response => {
                this.pid=this.$router.query.name
                if(this.pid !=null){
                  this.handleUpdate()
                }
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addHousemanager(this.form).then(response => {
                this.pid=this.$router.query.name
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      provinceChange(value){
        this.countrys= [];
        this.citys = [];
        this.queryParams.county = '';
        this.queryParams.city = '';
        getRegion(this.queryParams.province).then(response => {
          this.citys = response.data;
        });
      },
      cityChange(value){
        this.countrys= [];
        this.queryParams.county = '';
        getRegion(this.queryParams.city).then(response => {
          this.countrys = response.data;
        });
      },
      countryChange(value){

      },
      premissChange(){

      }
    }
  };
</script>
